#!/usr/bin/env python
# -*- coding: utf-8 -*-  

import os, struct
from os.path import join, normpath, isdir, isfile, abspath
import sys
import re
import time
import shutil
import string
import getopt

input_dir = ""
output_dir = ""

re_flow_c_file = r"^user_effect_flow_(.*).c$"
re_flow_source_item = r"^\s*\{(.*_(SOURCE_.*))\s*,\s*\d+\s*,\s*(BITS_16|BITS_24)\s*,\s*(CH_MONO|CH_STEREO).*\},.*"
re_flow_sink_item = r"^\s*\{(.*_(SINK_.*))\s*,\s*\d+\s*,\s*(BITS_16|BITS_24)\s*,\s*(CH_MONO|CH_STEREO).*\},.*"



ADAPT_HEADER_C = (
"/*"
"###############################################################################\n"
"# @file    roboeffect_adapt.c\n"
"# @author  castle (Automatic generated)\n"
"# @date    %s\n"
"# @brief   \n"
"# @attention\n"
"#\n"
"# THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS\n"
"# WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE\n"
"# TIME. AS A RESULT, MVSILICON SHALL NOT BE HELD LIABLE FOR ANY DIRECT,\n"
"# INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING\n"
"# FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE\n"
"# CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.\n"
"#\n"
"# <h2><center>&copy; COPYRIGHT 2023 MVSilicon </center></h2>\n"
"#/\n"
"###############################################################################\n"
"*/"
"\n\n\n"

"#include \"stdio.h\"\n"
"#include \"type.h\"\n"
"#include \"roboeffect_api.h\"\n"
"#include \"roboeffect_adapt.h\"\n"
"\n\n\n"

)

declaration_str = ""

ADAPT_HEADER_H = (
"#ifndef __ROBOEFFECT_ADAPT_H__\n"
"#define __ROBOEFFECT_ADAPT_H__\n\n\n"

"#include \"stdio.h\"\n"
"#include \"type.h\"\n"
"#include \"roboeffect_api.h\"\n\n\n"


"typedef struct _roboeffect_adapt_device_node\n"
"{\n"
"	uint32_t io_id;\n"
"	uint32_t width;\n"
"	uint32_t ch;\n"
"	char name[32];\n"
"}roboeffect_adapt_device_node;\n\n\n"

"typedef struct _roboeffect_adapt_device_table\n"
"{\n"
"	uint32_t count;\n"
"	const roboeffect_adapt_device_node table[];\n"
"}roboeffect_adapt_device_table;\n\n"

"%s\n"

"#endif/*__ROBOEFFECT_ADAPT_H__*/\n\n"


)


template_device_table = "const roboeffect_adapt_device_table %s_adapt_device_table = \n{\n"
template_device_table_declare = "extern const roboeffect_adapt_device_table %s_adapt_device_table;\n"

if __name__=="__main__":
		
	try:
		options,args = getopt.getopt(sys.argv[1:], "hi:o:", ["help", "inputdir=", "outputdir="])
	except getopt.GetoptError as err:
		print("arg error!")
		sys.exit(1)

	for name,value in options:
		if name in ("-i","--inputdir"):
			input_dir = value
		if name in ("-o", "--outputdir"):
			output_dir = value
		if name in ("-h","--help"):
			print ("\nThis is the usage function")
			print ('-i, --inputdir: input dir, folder to scan')
			print ('-o, --outputdir: output file folder')
			sys.exit(1)

	out_c = open(os.path.join(output_dir + 'roboeffect_adapt.c'), 'w+', encoding='utf-8')
	out_c.write(ADAPT_HEADER_C%("N/A"))


	for dirName, subdirList, fileList in os.walk(input_dir):
		for fname in fileList:
			pattern = re.compile(re_flow_c_file)
			matched = pattern.match(fname)
			if matched:
				print(fname)

				output_string = ""
				graphic_name = matched[1]

				out_c.write("#include \"%s\"\n"%(fname.replace(".c", ".h")))

				out_c.write(template_device_table%(graphic_name))
				declaration_str += template_device_table_declare%(graphic_name)

				items = ""
				items_counter = 0
				with open(os.path.join(dirName, fname)) as file_input: # ????

					lines = file_input.readlines()
					file_input.close()
					
					#source and sink
					pattern_src = re.compile(re_flow_source_item)
					pattern_sik = re.compile(re_flow_sink_item)
					for line in lines:
						matched_src = pattern_src.match(line)
						if(matched_src):
							items += "\t\t{\n"
							items += "\t\t\t%s,//%s\n"%(matched_src[1].replace(" ", ""), "io_id")
							items += "\t\t\t%s,//%s\n"%(matched_src[3].replace(" ", ""), "width")
							items += "\t\t\t%s,//%s\n"%(matched_src[4].replace(" ", ""), "channel")
							items += "\t\t\t\"%s\",//%s\n"%(matched_src[2].replace(" ", ""), "name")

							items += "\t\t},\n"
							items_counter += 1

						matched_src = pattern_sik.match(line)
						if(matched_src):
							items += "\t\t{\n"
							items += "\t\t\t%s,//%s\n"%(matched_src[1].replace(" ", ""), "io_id")
							items += "\t\t\t%s,//%s\n"%(matched_src[3].replace(" ", ""), "width")
							items += "\t\t\t%s,//%s\n"%(matched_src[4].replace(" ", ""), "channel")
							items += "\t\t\t\"%s\",//%s\n"%(matched_src[2].replace(" ", ""), "name")

							items += "\t\t},\n"
							items_counter += 1


				out_c.write("\t%d,\n"%(items_counter))
				out_c.write("\t{\n" + items + "\t}\n")
				out_c.write("\n};\n\n")
						


	out_c.close()

	out_c = open(os.path.join(output_dir + 'roboeffect_adapt.h'), 'w+', encoding='utf-8')
	out_c.write(ADAPT_HEADER_H%(declaration_str))
	out_c.close()


